/*     */ package com.zimbra.cs.mailbox;
/*     */ 
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.util.memcached.MemcachedKey;
/*     */ import com.zimbra.common.util.memcached.MemcachedMap;
/*     */ import com.zimbra.common.util.memcached.MemcachedSerializer;
/*     */ import com.zimbra.common.util.memcached.ZimbraMemcachedClient;
/*     */ import com.zimbra.cs.memcached.MemcachedConnector;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class MemcachedItemCache
/*     */ {
/*     */   public static class ItemCacheKey
/*     */     implements MemcachedKey
/*     */   {
/*     */     private String keyStr;
/*     */     
/*     */     public ItemCacheKey(Mailbox mbox, int itemId)
/*     */     {
/*  33 */       this.keyStr = (mbox.getAccountId() + ":" + mbox.getItemcacheCheckpoint() + ":" + itemId);
/*     */     }
/*     */     
/*     */     public boolean equals(Object other) {
/*  37 */       if ((other instanceof ItemCacheKey)) {
/*  38 */         ItemCacheKey otherKey = (ItemCacheKey)other;
/*  39 */         return this.keyStr.equals(otherKey.keyStr);
/*     */       }
/*  41 */       return false;
/*     */     }
/*     */     
/*     */     public int hashCode() {
/*  45 */       return this.keyStr.hashCode();
/*     */     }
/*     */     
/*     */ 
/*  49 */     public String getKeyPrefix() { return "zmMailItem:"; }
/*  50 */     public String getKeyValue() { return this.keyStr; }
/*     */   }
/*     */   
/*     */   public static class ItemCacheUuidKey implements MemcachedKey {
/*     */     private String keyStr;
/*     */     
/*     */     public ItemCacheUuidKey(Mailbox mbox, String uuid) {
/*  57 */       this.keyStr = (mbox.getAccountId() + ":" + mbox.getItemcacheCheckpoint() + ":" + uuid);
/*     */     }
/*     */     
/*     */     public boolean equals(Object other) {
/*  61 */       if ((other instanceof ItemCacheUuidKey)) {
/*  62 */         ItemCacheUuidKey otherKey = (ItemCacheUuidKey)other;
/*  63 */         return this.keyStr.equals(otherKey.keyStr);
/*     */       }
/*  65 */       return false;
/*     */     }
/*     */     
/*     */     public int hashCode() {
/*  69 */       return this.keyStr.hashCode();
/*     */     }
/*     */     
/*     */ 
/*  73 */     public String getKeyPrefix() { return "zmMailItem:"; }
/*  74 */     public String getKeyValue() { return this.keyStr; }
/*     */   }
/*     */   
/*  77 */   private static MemcachedItemCache sTheInstance = new MemcachedItemCache();
/*     */   
/*     */   private MemcachedMap<ItemCacheKey, Metadata> memcachedLookup;
/*     */   private MemcachedMap<ItemCacheUuidKey, Integer> memcachedUuidLookup;
/*     */   
/*  82 */   public static MemcachedItemCache getInstance() { return sTheInstance; }
/*     */   
/*     */   MemcachedItemCache() {
/*  85 */     ZimbraMemcachedClient memcachedClient = MemcachedConnector.getClient();
/*  86 */     this.memcachedLookup = new MemcachedMap(memcachedClient, new MailItemSerializer(), false);
/*  87 */     this.memcachedUuidLookup = new MemcachedMap(memcachedClient, new IntegerSerializer(null), false);
/*     */   }
/*     */   
/*     */   private static class IntegerSerializer implements MemcachedSerializer<Integer> {
/*  91 */     public Object serialize(Integer value) throws ServiceException { return value; }
/*  92 */     public Integer deserialize(Object obj) throws ServiceException { return (Integer)obj; }
/*     */   }
/*     */   
/*     */ 
/*     */   private static class MailItemSerializer
/*     */     implements MemcachedSerializer<Metadata>
/*     */   {
/*     */     public Object serialize(Metadata value)
/*     */     {
/* 101 */       return value.toString();
/*     */     }
/*     */     
/*     */     public Metadata deserialize(Object obj) throws ServiceException
/*     */     {
/* 106 */       return new Metadata((String)obj);
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public MailItem get(Mailbox mbox, int itemId)
/*     */     throws ServiceException
/*     */   {
/* 118 */     ItemCacheKey key = new ItemCacheKey(mbox, itemId);
/* 119 */     Metadata meta = (Metadata)this.memcachedLookup.get(key);
/* 120 */     if (meta != null) {
/* 121 */       MailItem.UnderlyingData ud = new MailItem.UnderlyingData();
/* 122 */       ud.deserialize(meta);
/* 123 */       return MailItem.constructItem(mbox, ud, true);
/*     */     }
/* 125 */     return null;
/*     */   }
/*     */   
/*     */   public MailItem get(Mailbox mbox, String uuid) throws ServiceException
/*     */   {
/* 130 */     ItemCacheUuidKey key = new ItemCacheUuidKey(mbox, uuid);
/* 131 */     Integer itemId = (Integer)this.memcachedUuidLookup.get(key);
/* 132 */     if (itemId != null) {
/* 133 */       return get(mbox, itemId.intValue());
/*     */     }
/* 135 */     return null;
/*     */   }
/*     */   
/*     */   public void put(Mailbox mbox, MailItem item) throws ServiceException
/*     */   {
/* 140 */     ItemCacheKey key = new ItemCacheKey(mbox, item.getId());
/* 141 */     this.memcachedLookup.put(key, item.serializeUnderlyingData());
/* 142 */     ItemCacheUuidKey uuidKey = new ItemCacheUuidKey(mbox, item.getUuid());
/* 143 */     this.memcachedUuidLookup.put(uuidKey, Integer.valueOf(item.getId()));
/*     */   }
/*     */   
/*     */   public MailItem remove(Mailbox mbox, int itemId) throws ServiceException {
/* 147 */     MailItem item = get(mbox, itemId);
/* 148 */     if (item != null) {
/* 149 */       ItemCacheKey key = new ItemCacheKey(mbox, itemId);
/* 150 */       this.memcachedLookup.remove(key);
/* 151 */       ItemCacheUuidKey uuidKey = new ItemCacheUuidKey(mbox, item.getUuid());
/* 152 */       this.memcachedUuidLookup.remove(uuidKey);
/* 153 */       return item;
/*     */     }
/* 155 */     return null;
/*     */   }
/*     */   
/*     */   public void purgeMailbox(Mailbox mbox) {}
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/mailbox/MemcachedItemCache.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */